﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UtilLib
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static partial class Util
    {
        /// <summary>
        /// Determines whether any flag is on for the specified mask.
        /// </summary>
        /// <typeparam name="T">The flag type.</typeparam>
        /// <param name="mask">The mask to check if the flag is on.</param>
        /// <param name="flag">The flag to check for in the mask.</param>
        /// <returns>
        /// 	<c>true</c> if any flag is on for the specified mask; otherwise, <c>false</c>.
        /// </returns>
        public static bool FlagIsAnyOn<T>(this long mask, T flag)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            ulong flagInt = Convert.ToUInt64(flag);
            ulong maskInt = Convert.ToUInt64(mask);

            return (maskInt & flagInt) != 0;
        }

        /// <summary>
        /// Determines whether the flag is on for the specified mask.
        /// </summary>
        /// <typeparam name="T">The flag type.</typeparam>
        /// <param name="mask">The mask to check if the flag is on.</param>
        /// <param name="flag">The flag to check for in the mask.</param>
        /// <returns>
        /// 	<c>true</c> if the flag is on for the specified mask; otherwise, <c>false</c>.
        /// </returns>
        public static bool FlagIsOn<T>(this long mask, T flag)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            ulong flagInt = Convert.ToUInt64(flag);
            ulong maskInt = Convert.ToUInt64(mask);

            return (maskInt & flagInt) == flagInt;
        }

        /// <summary>
        /// Sets the flag on in the specified mask.
        /// </summary>
        /// <typeparam name="T">The flag type.</typeparam>
        /// <param name="mask">The mask to set flag on.</param>
        /// <param name="flag">The flag to set.</param>
        /// <returns>The mask with the flag set to on.</returns>
        public static long FlagSetOn<T>(this long mask, T flag)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            ulong flagInt = Convert.ToUInt64(flag);
            ulong maskInt = Convert.ToUInt64(mask);

            maskInt |= flagInt;

            return (long)maskInt;
        }

        /// <summary>
        /// Sets the flag off in the specified mask.
        /// </summary>
        /// <typeparam name="T">The flag type.</typeparam>
        /// <param name="mask">The mask to set flag off.</param>
        /// <param name="flag">The flag to set.</param>
        /// <returns>The mask with the flag set to off.</returns>
        public static long FlagSetOff<T>(this long mask, T flag)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            ulong flagInt = Convert.ToUInt64(flag);
            ulong maskInt = Convert.ToUInt64(mask);

            maskInt &= ~flagInt;

            return (long)maskInt;
        }

        /// <summary>
        /// Toggles the flag in the specified mask.
        /// </summary>
        /// <typeparam name="T">The flag type.</typeparam>
        /// <param name="mask">The mask to toggle the flag against.</param>
        /// <param name="flag">The flag to toggle.</param>
        /// <returns>The mask with the flag set in the opposite position then it was.</returns>
        public static long FlagToggle<T>(this long mask, T flag)
            where T : struct, IComparable, IFormattable, IConvertible
        {
            ulong flagInt = Convert.ToUInt64(flag);
            ulong maskInt = Convert.ToUInt64(mask);

            maskInt ^= flagInt;

            return (long)maskInt;
        }

    }
}
